package edu.demo.recommend.service.impl;

import edu.demo.recommend.mapper.CategoryMapper;
import edu.demo.recommend.common.ResultCodeEnum;
import edu.demo.recommend.exception.RecommendBusinessException;
import edu.demo.recommend.pojo.Category;
import edu.demo.recommend.service.CategoryService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hb
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    @Override
    @Transactional
    public Category create(Category category) {
        try {
            categoryMapper.insertSelective(category);
        }catch (DuplicateKeyException e) {
            throw new RecommendBusinessException(ResultCodeEnum.REGIST_DUP_FAIL);
        }
         return category;
    }

    @Override
    public Category getById(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Category> selectAll() {
        return categoryMapper.selectAll();
    }
}
